package com.example.controller;

import com.example.pojo.PageBean;
import com.example.pojo.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserCtrl {

    @Autowired
    private UserService userService;

//    @RequestMapping("list")
//    public Map<String,Object> userList(PageBean pageBean, User user){
//        Map<String,Object> resultMap=new HashMap<>();
//        List<User> userList = userService.getUserByPage(pageBean, user);
//        resultMap.put("userList",userList);
//        resultMap.put("pageBean",pageBean);
//    }

    @RequestMapping("list")
    public String userList(Model model,PageBean pageBean,User user){
        List<User> userList = userService.getUserByPage(pageBean, user);
        System.out.println(userList);
        model.addAttribute("userList",userList);
        model.addAttribute("pageBean",pageBean);
        return "list";

    }
    @RequestMapping("regDo")
    @ResponseBody
    public Map<String,Object> saveUser(User user){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            userService.reg(user);
            resultMap.put("result",true);
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
    @RequestMapping("regName")
    @ResponseBody
    public Map<String,Object> cekName(User user){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            Integer i=userService.findOne(user);
            if (i > 0) {
                resultMap.put("result",true);
            }else {
                resultMap.put("result",false);
            }
        }catch (Exception e){
            e.printStackTrace();
            resultMap.put("result",false);
        }
        return resultMap;
    }
}
